<?php

// ------------------------------------------------------------------------
// |@Author       : Jarmin <topextend@qq.com>
// |@----------------------------------------------------------------------
// |@Date         : 2025-05-20 17:57:20
// |@----------------------------------------------------------------------
// |@LastEditTime : 2025-05-20 18:15:38
// |@----------------------------------------------------------------------
// |@LastEditors  : Jarmin <topextend@qq.com>
// |@----------------------------------------------------------------------
// |@Description  : 验证码扩展类，提供验证码的生成、验证等功能
// |@----------------------------------------------------------------------
// |@FilePath     : CaptchaExtend.php
// |@----------------------------------------------------------------------
// |@Copyright (c) 2025 http://www.ladmin.cn All Rights Reserved.
// ------------------------------------------------------------------------
declare (strict_types=1);

namespace think\admin\extend;

use think\Exception;
use think\captcha\service\Service;
use think\captcha\service\ClickWordCaptchaService;
use think\captcha\service\BlockPuzzleCaptchaService;

/**
 * 验证码扩展类
 * @class CaptchaExtend
 * @package think\admin\extend
 */
class CaptchaExtend
{
    /**
     * 验证码生成器
     * @param string $type 验证码类型，支持 block_puzzle(滑动拼图) 和 click_word(点选文字)
     * @return array 返回验证码数据，包含 token、secretKey 等
     * @throws Exception 当验证码类型无效时抛出
     */
    public static function create(string $type = 'block_puzzle'): array
    {
        return self::getCaptchaService($type)->get();
    }

    /**
     * 一次验证
     * @param string $token 验证token
     * @param string $pointJson 验证坐标数据（加密后的JSON字符串）
     * @param string $type 验证码类型，支持 block_puzzle(滑动拼图) 和 click_word(点选文字)
     * @return bool 返回验证结果，true表示验证通过，false表示验证失败
     * @throws Exception 当验证码类型无效时抛出
     */
    public static function check(string $token, string $pointJson, string $type = 'block_puzzle'): bool
    {
        return self::getCaptchaService($type)->check($token, $pointJson);
    }

    /**
     * 二次验证
     * @param string $token 验证token
     * @param string $pointJson 验证坐标数据（加密后的JSON字符串）
     * @param string $type 验证码类型，支持 block_puzzle(滑动拼图) 和 click_word(点选文字)
     * @return bool
     * @throws Exception 当验证码类型无效时抛出
     */
    public static function verify(string $token, string $pointJson, string $type = 'block_puzzle'): bool
    {
        return self::getCaptchaService($type)->verification($token, $pointJson);
    }

    /**
     * 获取验证码服务实例
     * @param string $type 验证码类型，支持 block_puzzle(滑动拼图) 和 click_word(点选文字)
     * @return Service 返回验证码服务实例
     * @throws Exception 当验证码类型无效时抛出
     */
    public static function getCaptchaService(string $type): Service
    {
        $config = config('captcha');
        switch ($type) {
            case 'click_word':
                return new ClickWordCaptchaService($config);
            case 'block_puzzle':
                return new BlockPuzzleCaptchaService($config);
            default:
                throw new Exception(lang('service.captcha_type_invalid', ['type' => $type]));
        }
    }
}
